﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using KPIS.GERP.OMVC.MessageService;
using KPIS.GERP.Models.SYS;
using KPIS.GERP.Models.SYS.ViewModel;
using KPIS.GERP.OMVC.Classes;
using KPIS.GERP.OMVC.EmployeeService;
using KPIS.GERP.OMVC.SysApprovalService;
using KPIS.GERP.OMVC.UserService;

namespace KPIS.GERP.WEB.MVC.Controllers
{
    public class ApprovalTaskController : Controller
	{
        private MessageServiceClient messageServiceClient = new MessageServiceClient(); 
        private SysApprovalServiceClient serviceClient = new SysApprovalServiceClient();
        private UserServiceClient userServiceClient = new UserServiceClient();
        private EmployeeServiceClient employeeServiceClient = new EmployeeServiceClient();
        public SsMng ssMng = new SsMng();

		public ActionResult Index()
		{
			List<ApprovalTask> obj;
			obj = (from t in serviceClient.SelectApprovalTasks(ssMng.EmpSeqI)
					   select t).OrderByDescending(t => t.ApprovalTaskId).ToList();
			 
			return View(obj);
		}

        public ActionResult TaskInbox()
        {
            List<ApprovalTask> obj;
            obj = (from t in serviceClient.SelectApprovalTasks(ssMng.EmpSeqI)
                   select t).OrderByDescending(t => t.ApprovalTaskId).ToList();

            return View(obj);
        }

        public ActionResult TaskMessageInbox(int? ruleApproveGroupSeq)
        {
            ViewBag.RuleApproveGroupSeq = ruleApproveGroupSeq;
            return View();
            //var obj = messageServiceClient.SelectMessage(null, null);
            //ViewBag.empList = employeeServiceClient.Select(null,null,true);
            //ViewBag.userList = userServiceClient.GetUsers();
            //ViewBag.ruleApproveGroupList = serviceClient.SelectApproveProcessGroup();
            //ViewBag.ruleApproveTypeList = serviceClient.SelectApproveProcess(null); 

            //if (ruleApproveGroupSeq == null || ruleApproveGroupSeq == 0) return View(obj);

            //var ruleApproveGroupFiltered = serviceClient.SelectApproveProcessGroup().FirstOrDefault(o => o.AppPrcGrpId == ruleApproveGroupSeq.Value);

            //if (ruleApproveGroupFiltered == null)
            //{
            //    return RedirectToAction("TaskMessageInbox");
            //}
            //ViewBag.RuleApproveGroupFiltered = ruleApproveGroupFiltered;


            //return View();
        }

        [HttpPost]
        public ActionResult TakeAction(int taskMessageSeq, int wfInstantId, string actionCode, string Reasons, string MessageTitle = null, string backto = "",
             string MessageTitleA = null,string MessageTitleF = null,string MessageTitleN = null,string MessageTitleJ = null,string MessageTitleC = null)
		{
			try
            {
		    KPIS.GERP.Models.SYS.CommandAction commandAction;
		    switch (actionCode)
		    {
                case "A" : commandAction = CommandAction.Approve;
		            break;
                case "F" : commandAction = CommandAction.Forward;
		            break;
                case "N" : commandAction = CommandAction.SendBack;
		            break;
                case "J" : commandAction = CommandAction.Reject;
		            break;
                case "C" : commandAction = CommandAction.Cancel;
		            break;
                    default: commandAction = CommandAction.Information;
		            break;
		    }
                MessageTitle = MessageTitle ?? MessageTitleA ?? MessageTitleF ?? MessageTitleN ?? MessageTitleJ ?? MessageTitleC;
                var commandActionVm = new CommandActionVM();
                commandActionVm.CommandAction = commandAction;
                commandActionVm.MessageId = taskMessageSeq;
                commandActionVm.ByEmpId = ssMng.EmpSeqI;
                commandActionVm.NewTitle = MessageTitle;
                commandActionVm.Remarks = Reasons;
                serviceClient.ExecuteCommandAction(commandActionVm);
			}
			catch (Exception ex)
			{
                return RedirectToAction("TaskMessageDetails", new
                    {
                        messageFromTakeAction = System.Web.HttpUtility.HtmlEncode("ไม่สามารถทำคำสั่งกับคำขออนุมัตินี้ได้ อาจเกิดจากสถานะคำขอ: <br/>" +
                                                "1. สิ้นสุดลงแล้ว<br/>" +
                                                "2. คำขออนุมัติในระบบ Workflow<br/>" +
                                                "3. ถูกล้างข้อมูลหรือลบไปจากระบบ<br/>"),
                        messageFromTakeActionHide = ex.Message,
                        taskMessageSeq = taskMessageSeq
                    });
			}
			return RedirectToAction("Index");
		}

		public ActionResult Details(int approvalTaskSeq)
		{ 
				var obj = (from t in serviceClient.SelectApprovalTasks(ssMng.EmpSeqI)
						   where t.ApprovalTaskId == approvalTaskSeq
						   select t).OrderByDescending(t => t.ApprovalTaskId).FirstOrDefault();

				return View(obj); 
		}

        public ActionResult TaskMessageDetails(int taskMessageSeq, int? ruleApproveGroupSeq, string messageFromTakeAction = "", string messageFromTakeActionHide = "")
        {
            ViewBag.TaskMessageSeq = taskMessageSeq;
            ViewBag.MessageFromTakeAction = messageFromTakeAction;
            ViewBag.MessageFromTakeActionHide = messageFromTakeActionHide;
            var message = messageServiceClient.SelectMessage(new Message{MessageId = taskMessageSeq}, null).FirstOrDefault();
                ViewBag.MessageModel = message;
                ViewBag.RuleApproveGroupSeq = ruleApproveGroupSeq;
                messageServiceClient.ReadMessage(taskMessageSeq, ssMng.EmpSeqI);

            var approvalTaskDtlId = serviceClient.SelectApprovalTaskDtls(null, message.ApprovalTaskDtlId).FirstOrDefault().ApprovalTaskId;

                List<ApprovalTask> taskList; 
                    taskList = serviceClient.SelectApprovalTasks(ssMng.EmpSeqI).ToList(); 
                var obj = (from t in taskList
                           where t.ApprovalTaskId == approvalTaskDtlId
                           select t).OrderByDescending(t => t.ApprovalTaskId).FirstOrDefault();


                return View(obj);
        }
         
        public ActionResult RuleApproveList()
        {
            ViewBag.empList = employeeServiceClient.Select(null,null,true);
            ViewBag.userList = userServiceClient.GetUsers();
            ViewBag.ruleApproveGroupList = serviceClient.SelectApproveProcessGroup();
            ViewBag.ruleApproveTypeList = serviceClient.SelectApproveProcess(null);
            return View();
        }


    }
}
